package 线程;
/*
业务需求：四个线程窗口共同售卖100门票
方式一：继承
 */
public class TestThread1改良 {//1~~100
    public static void main(String[] args) {
        //1.多线程对象创建
        Ticket t1 = new Ticket();
        Ticket t2 = new Ticket();
        Ticket t3 = new Ticket();
        Ticket t4 = new Ticket();
        t1.start();//记住启动线程不能使用run方法
        t2.start();
        t3.start();
        t4.start();

    }
}
//1.继承
class Ticket extends Thread{
    //2.声明票数
  static int tickets =100;
    @Override//3
    public void run() {
        //4；线程安全-->加同步锁;锁格式synchronized (){}参数为锁对象:类名.class
        synchronized (Ticket.class){
            while (true){//5
                if(tickets>0) {
                    try {
                        Thread.sleep(10);//6
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //7.打印输出
                    System.out.println(getName() + "=" + tickets--);
                }
                if(tickets<=0) break;//8.结束循环
            }
        }

    }
}